package logic

import (
	"context"
	"goods_srv/model/mysql"

	"goods_srv/goods"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *goods.GoodsListRequest) (*goods.GoodsListResponse, error) {
	// todo: 商品列表

	g := mysql.GoodsZye3{}
	list, err := g.GoodsList(in.Keyword, int(in.Page), int(in.PageSize))
	if err != nil {
		logx.Error("列表查询失败", err)
		return nil, err
	}

	var GoodsList []*goods.GoodsL

	for _, v := range list {
		GoodsList = append(GoodsList, &goods.GoodsL{
			GoodsTitle:  v.GoodsTitle,
			GoodsPrice:  v.GoodsPrice,
			GoodsStock:  v.GoodsStock,
			GoodsStatus: int64(v.GoodsStatus),
			GoodsAvatar: v.GoodsAvatar,
			GoodsId:     int64(v.ID),
		})
	}
	return &goods.GoodsListResponse{GoodsList: GoodsList}, nil
}
